//
//  BBCompositeView.m
//  BBTouch
//
//  Created by ben smith on 8/13/08.
//  This file is part of BBTouch.
//
//  BBTouch is free software: you can redistribute it and/or modify
//  it under the terms of the GNU Lesser General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.

//  BBTouch is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU Lesser General Public License for more details.

//  You should have received a copy of the GNU Lesser General Public License
//  along with BBTouch.  If not, see <http://www.gnu.org/licenses/>.
// 
//  Copyright 2008 Ben Britten Smith ben@benbritten.com .
//

#import "BBCompositeView.h"
#import "BBCameraView.h"
#import "BBRawBlobView.h"
#import "BBProjectionView.h"
#import "BBTestAppView.h"
#import "BBPlaceholderView.h"
#import "BBInputController.h"

@implementation BBCompositeView

@synthesize borrowedPlaceholder;

-(void)awakeFromNib
{
	// register for this notification so i can get set up my view when the app is all loaded
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appBegin:) name:@"NSApplicationDidFinishLaunchingNotification" object:nil];
}

-(NSView*)currentView
{
	return [views objectAtIndex:0];
}

-(void)borrowView:(NSView*)aView
{
	// need to remove this view from my hierarchy, someone else wants it
	[aView removeFromSuperview];
	BBPlaceholderView * placeholder = [[BBPlaceholderView alloc] initWithFrame:[aView frame]];
	NSInteger index = [views indexOfObject:aView];
	[views replaceObjectAtIndex:index withObject:placeholder];
	[self addSubview:placeholder];
	self.borrowedPlaceholder = placeholder;
	[placeholder release];
	[self shuffleViews:[views objectAtIndex:1]]; // grab the next one, doesnt really matter
}

-(void)returnView:(NSView*)aView
{
	[self.borrowedPlaceholder removeFromSuperview];
	[views removeObject:self.borrowedPlaceholder];
	[views addObject:aView];
	[self addSubview:aView];
	[self shuffleViews:aView]; 
}


-(void)appBegin:(id)sender
{
	[[NSNotificationCenter defaultCenter] removeObserver:self];
	// build the view rectangles
	NSRect bigRect = [self bounds];
	// we want one big view on the right and three small ones on the left
	NSRect bigRight = NSMakeRect(NSWidth(bigRect)/4.0, 0.0, NSWidth(bigRect) * 0.75, NSHeight(bigRect));
	
	NSRect upperLeft = NSMakeRect(0.0, NSHeight(bigRect) * 0.6666, NSWidth(bigRect)/4.0, NSHeight(bigRect)/3.0);
	NSRect middleLeft = NSMakeRect(0.0, NSHeight(bigRect) * 0.3333, NSWidth(bigRect)/4.0, NSHeight(bigRect)/3.0);
	NSRect lowerLeft = NSMakeRect(0.0, 0.0, NSWidth(bigRect)/4.0, NSHeight(bigRect)/3.0);
	
	// now load them into the array
	viewFrames = [[NSMutableArray alloc] init];
	[viewFrames addObject:[NSValue valueWithRect:bigRight]];
	[viewFrames addObject:[NSValue valueWithRect:upperLeft]];
	[viewFrames addObject:[NSValue valueWithRect:middleLeft]];
	[viewFrames addObject:[NSValue valueWithRect:lowerLeft]];
	
	// now build the views
	BBCameraView * camView = [[BBCameraView alloc] initWithFrame:bigRight];
	BBRawBlobView * rawView = [[BBRawBlobView alloc] initWithFrame:upperLeft];
	BBProjectionView * projView = [[BBProjectionView alloc] initWithFrame:middleLeft];
	// calibration view
	BBTestAppView * testView = [[BBTestAppView alloc] initWithFrame:lowerLeft];
	
	// add the views to the array
	views = [[NSMutableArray alloc] init];
	[views addObject:camView];
	[views addObject:rawView];
	[views addObject:projView];
	[views addObject:testView];
	
	// now add the views to the main view
	[self addSubview:camView];
	[self addSubview:rawView];
	[self addSubview:projView];
	[self addSubview:testView];

	[[[self window] contentView] addSubview:cameraViewSettings];
	[[[self window] contentView] addSubview:filteredBlobViewSettings];
	[[[self window] contentView] addSubview:projectionViewSettings];
	
	
	[self resetSettingsViews];
	[[BBInputController sharedInputController] setRawBlobView:rawView];

	[camView release];
	[rawView release];
	[projView release];
	[testView release];
}

-(void)mouseDown:(NSEvent*)theEvent
{
	// check to see if we hit any of the small views
	[self didHandleMouseDown:theEvent];
}

-(BOOL)didHandleMouseDown:(NSEvent*)theEvent
{
	NSPoint mouseLoc = [[[self window] contentView] convertPoint:[theEvent locationInWindow] toView:self];
	int i;
	// go through the 'small' views and see if we hit
	for (i = 1; i < [views count]; i++) {
		if (NSPointInRect(mouseLoc, [[views objectAtIndex:i] frame])) {
			// great, we hit one of our small views,
			// we need to swap this view with the big one
			// this isn't the 'best' way to do this, but
			// it makes for a nice animation
			[self shuffleViews:[views objectAtIndex:i]];
			return YES;
		}
	}	
	return NO;
}

-(void)shuffleViews:(NSView*)newMainView
{
	id bigView = [views objectAtIndex:0];		
	[bigView resignFirstResponder];
	[views removeObject:bigView];			
	[views removeObject:newMainView];			
	[views insertObject:newMainView atIndex:0];
	[views addObject:bigView];
	// now reset all the view frames
	[self resetFrameRects];
	[self resetSettingsViews];
}

-(void)resetSettingsViews
{
	// this is a bit of manual hackery
	NSPoint offscreen = NSMakePoint(0, -200);
	NSPoint onscreen = NSMakePoint(5, 5);
	id bigView = [views objectAtIndex:0];			
	if ([bigView isKindOfClass:[BBCameraView class]]) {
		[[cameraViewSettings animator] setFrameOrigin:onscreen];
		[[filteredBlobViewSettings animator] setFrameOrigin:offscreen];		
		[[projectionViewSettings animator] setFrameOrigin:offscreen];
		return;
	}
	if ([bigView isKindOfClass:[BBRawBlobView class]]) {
		[[cameraViewSettings animator] setFrameOrigin:offscreen];
		[[filteredBlobViewSettings animator] setFrameOrigin:onscreen];		
		[[projectionViewSettings animator] setFrameOrigin:offscreen];
		return;
	}
	if ([bigView isKindOfClass:[BBProjectionView class]]) {
		[[cameraViewSettings animator] setFrameOrigin:offscreen];
		[[filteredBlobViewSettings animator] setFrameOrigin:offscreen];		
		[[projectionViewSettings animator] setFrameOrigin:onscreen];
		return;
	}
	
	[[filteredBlobViewSettings animator] setFrameOrigin:offscreen];		
	[[cameraViewSettings animator] setFrameOrigin:offscreen];		
	[[projectionViewSettings animator] setFrameOrigin:offscreen];
}


-(void)resetFrameRects
{
	int viewIndex;
	for (viewIndex = 0; viewIndex < [views count]; viewIndex++) {
		NSRect newFrame = [[viewFrames objectAtIndex:viewIndex] rectValue];
		[[[views objectAtIndex:viewIndex] animator] setFrame:newFrame];
//		[[views objectAtIndex:viewIndex] setFrame:newFrame];
	}
}

@end
